﻿package com.oyjd.beauty.business.kid.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import com.oyjd.beauty.bean.vo.KidneyVO;
import com.oyjd.beauty.bean.vo.UserVO;
import com.oyjd.beauty.business.kid.service.KidService;
import com.oyjd.beauty.fw.action.BaseAction;
import com.oyjd.beauty.fw.action.Page;
import com.oyjd.beauty.fw.util.CodeUtil;
import com.oyjd.beauty.fw.util.OutUtil;
import com.oyjd.beauty.pub.Const;

/**
 * 
 * 性格信息Action控制层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Action(value = "/kid")
@Results({ @Result(name = "viewList", location = Const.JSP_kid_list), @Result(name = "viewAdd", location = Const.JSP_kid_add), @Result(name = "viewEdit", location = Const.JSP_kid_edit) })
public class KidAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 45354347027015039L;

	private static final Log log = LogFactory.getLog(KidAction.class);
	private KidService service;

	@Autowired
	public void setService(KidService service) {
		this.service = service;
	}

	/**
	 * 进入性格信息列表页面
	 * 
	 * @return
	 */
	public String viewList() {
		return "viewList";
	}

	/**
	 * 进入性格信息新增页面
	 * 
	 * @return
	 */
	public String viewAdd() {
		return "viewAdd";
	}

	/**
	 * 进入性格信息修改页面
	 * 
	 * @return
	 */
	public String viewEdit() {
		try {
			HttpServletRequest req = getRequest();
			String id = ServletRequestUtils.getStringParameter(req, "id");
			KidneyVO vo = (KidneyVO) service.getKidneyVO(id);
			req.setAttribute("data", vo);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewEdit";
	}

	/**
	 * 获取性格信息列表数据
	 * 
	 * @return
	 */
	public String getList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			Page<KidneyVO> page = service.getList(new Page<KidneyVO>(req));
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 新增性格信息
	 * 
	 * @return
	 */
	public String add() {
		try {
			HttpServletRequest req = getRequest();
			String name = ServletRequestUtils.getStringParameter(req, "name");
			String note = ServletRequestUtils.getStringParameter(req, "note");

			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.add(name, note, userVO.getAccount());
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewList";
	}

	/**
	 * 编辑性格信息
	 * 
	 * @return
	 */
	public String edit() {
		try {
			HttpServletRequest req = getRequest();
			String kidId = ServletRequestUtils.getStringParameter(req, "kidId");
			String name = ServletRequestUtils.getStringParameter(req, "name");
			String note = ServletRequestUtils.getStringParameter(req, "note");

			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.edit(kidId, name, note, userVO.getAccount());
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewList";
	}

	/**
	 * 删除性格信息
	 * 
	 * @return
	 */
	public String remove() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String[] cbs = ServletRequestUtils.getStringParameters(req, "cb");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.remove(cbs, userVO.getAccount());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}
}
